<template>
  <view class="mycode">
      <view class="flex-col group_1">
        <view class="flex-row group_2">
          <view class="flex-col group_3">
            <view class="flex-col items-end group_4">
              <text class="text_1">分享领取佣金</text>
            </view>
            <view class="justify-center group_5">
              <view class="section_1"><!--*--></view>
              <text class="text_2 text_3">分享好友团队增员</text>
              <view class="section_1 view_1"><!--*--></view>
            </view>
          </view>
        </view>
        <view class="flex-row group_6">
          <view class="flex-col section_5"   v-show="!box_show">
            <text class="text_2 text_4">邀请码</text>
            <canvas
              canvas-id="myCanvas"
              :style="'width:' + windowWidth + 'px;height:' + windowHeight + 'px;margin: 0 auto'"
            />
            <view class="flex-col items-center group_8">
              <image
                src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/62ac1d455a7e3f03100b6468/62b188edd029820011534ee0/16558042060163739745.png"
                class="image_20"
              />
              <text class="text_5">点击分享发送给好友即可</text>
              <view class="flex-col items-center button" @click="box_show = true"><text class="text_6">分享</text></view>
            </view>
          </view>
        </view>
      </view>
    <view class="box" v-show="box_show">
      <image :src="ewmImg" style="width: 400rpx;height: 400rpx;"></image>
      <view class="box_c">
        <view>①保存图片到相册</view>
        <view>②分享图片给朋友</view>
      </view>
      <view class="box_b">
        <button type="default" class="btn2" @click="box_show = false">取 消</button>
        <button type="primary" class="btn2" @click="saveEwm">保 存</button>
      </view>
    </view>
  </view>
</template>

<script>
    import { getqrcode } from '@/api/index.js'
    import {
    	base64src
    } from '@/components/utils/base64src.js'
    import {spreadBanner} from "../../api";
export default {
  data() {
    return {
      url: '',
      myData: {
        name: '皮皮',
        explainInfo: '分享图片到朋友技术测试',
      },
      openSettingBtnHidden: true, //是否授权
      ewmImg: '', //这是要保存的图片
      windowWidth: 200,
      windowHeight: 200,
      box_show: false,
    }
  },
  onLoad() {
    spreadBanner().then(res => {
      console.log('res', res)
      base64src(res.data, res => {
        that.ewmImg= res;
      });
      setTimeout(() => {
        that.saveImage(that.ewmImg)
      }, 300);
    })

  },
  methods: {
    // 分享图片到朋友，url路径需要一个在线路径

    fx() {
      let _this = this
      console.log('点击了分享', _this.ewmImg)
      wx.downloadFile({
        url: _this.ewmImg,
        success: res => {
          wx.showShareImageMenu({
            path: res.tempFilePath,
          })
        },
        fail: err => {
          console.log('错误信息', err)
        },
      })
    },

    //微信小程序保存到相册
    handleSetting(e) {
      if (!e.detail.authSetting['scope.writePhotosAlbum']) {
        _this.openSettingBtnHidden = false
      } else {
        _this.openSettingBtnHidden = true
      }
    },
    saveEwm: function(e) {
      let _this = this
      //获取相册授权
      uni.getSetting({
        success(res) {
          if (!res.authSetting['scope.writePhotosAlbum']) {
            uni.authorize({
              scope: 'scope.writePhotosAlbum',
              success() {
                //这里是用户同意授权后的回调
                _this.saveImgToLocal()
              },
              fail() {
                //这里是用户拒绝授权后的回调
                _this.openSettingBtnHidden = false
              },
            })
          } else {
            //用户已经授权过了
            _this.saveImgToLocal()
          }
        },
      })
    },
    saveImgToLocal: function(e) {
      let _this = this
      console.log('图片路径', _this.ewmImg)
      uni.showModal({
        title: '提示',
        content: '确定保存到相册吗',
        success: function(res) {
          if (res.confirm) {
            uni.saveImageToPhotosAlbum({
              filePath: _this.ewmImg,
              success: function(res) {
                uni.showToast({ title: '图片已保存' })
                // console.log(1111, filePath)
              },
            })
          } else if (res.cancel) {
          }
        },
      })
    },

    //小程序端保存图片
    saveImage(sa) {
        console.log(sa)
      let _this = this
      uni.showLoading({
        title: '图片绘制中...',
      })

    const context = uni.createCanvasContext('myCanvas')
      wx.getImageInfo({
      	src: sa,
      	success: function(res) {
            context.drawImage(res.path, 0, 0, 200, 200)
            context.setFontSize(20)
            context.fillStyle = '#ffffff'
            context.draw(true, function() {
              setTimeout(() => {
                uni.canvasToTempFilePath(
                  {
                    fileType: 'png',
                    canvasId: 'myCanvas',
                    success: function(res) {
                      console.log('结果', res)
                      uni.hideLoading()
                      _this.ewmImg = res.tempFilePath
                    },
                  },
                  this
                )
              }, 1000)
            })
      	},
        fail(e) {
            console.log("失败",e);
        }
      })

    },
  },
}
</script>

<style lang="less" scoped>
.mycode {
  width: 100%;
  height: 100vh;
  background-color: #fff;
  box-sizing: border-box;
  overflow: hidden;

  .btn1 {
    width: 80%;
    margin: 48rpx auto 24rpx;
    color: #fff;
    background: linear-gradient(to right, #006fff 0%, #189dff 100%);
  }
  .box {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 99999;
    background-color: rgba(0, 0, 0, 0.9);
    image {
      display: block;
      width: 300px;
      height: 400px;
      margin: 50px auto;
    }
    .box_c {
      width: 80%;
      height: 60px;
      background-color: #fff;
      border-radius: 10px;
      margin: 0 auto;
      padding-left: 20px;
      padding-top: 10px;
      box-sizing: border-box;
    }
    .box_b {
      position: fixed;
      bottom: 0;
      width: 100%;
      height: 80px;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .btn2 {
        width: 40%;
      }
    }
  }
  .foot {
    position: fixed;
    width: 100%;
    text-align: center;
    font-size: 22rpx;
    font-weight: 500;
    color: #666;
    bottom: 20rpx;
    left: 0;
  }
}
  .text_2 {
    text-transform: uppercase;
  }
  .section_1 {
    align-self: center;
    background-color: #a17356;
    width: 2.09rem;
    height: 0.063rem;
  }
  .page {
    background-color: #ffffff;
    /* background-image: linear-gradient(270deg, #fbe3cf 0%, #f9d0a6 100%); */
    height: 100%;
    width: 100%;
    overflow-y: auto;
    overflow-x: hidden;
  }
  .group {
    margin-top: -0.69rem;
    align-self: flex-start;
    overflow-x: hidden;
    width: 13.88rem;
    position: relative;
  }
  .group_1 {
    padding: 5rem 0 2rem;
    overflow-x: hidden;
   background: url('https://ceshi2.cnshangji.cn/zu.png');
    // background-color: #006fff;
  }
  .image-wrapper {
    margin-left: -0.91rem;
    padding-top: 2.66rem;
    flex-shrink: 0;
    background-image: url('https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/62ac1d455a7e3f03100b6468/62b188edd029820011534ee0/16558042057714315895.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    width: 3.75rem;
    height: 3.75rem;
  }
  .text {
    margin-left: 6.78rem;
    margin-top: 2.72rem;
    flex-shrink: 0;
    color: #000;
    font-size: 1.06rem;
    font-family: SourceHanSansCN;
    font-weight: 500;
    line-height: 1.03rem;
  }
  .group_2 {
    align-self: flex-start;
    overflow-x: hidden;
    width: 18.69rem;
  }
  .group_6 {
    margin-top: 0.75rem;
    overflow-x: hidden;
    height: 32.56rem;
    position: relative;
  }
  .image {
    width: 0.59rem;
    height: 1.09rem;
  }
  .image_1 {
    margin-left: -1.31rem;
    flex-shrink: 0;
    align-self: center;
    width: 2.66rem;
    height: 3.13rem;
  }
  .group_3 {
    margin-left: 2.72rem;
    flex-shrink: 0;
    width: 20rem;
  }
  .image_3 {
    width: 11.09rem;
    height: 7.41rem;
    position: absolute;
    left: -7.53rem;
    bottom: 6.88rem;
  }
  .section_2 {
    padding: 3.94rem 0 0.53rem;
    background-image: url('https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/62ac1d455a7e3f03100b6468/62b188edd029820011534ee0/16558042044732799181.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    width: 17.31rem;
    position: absolute;
    left: -3.59rem;
    top: 23.69rem;
  }
  .image_5 {
    width: 1.31rem;
    height: 1.59rem;
    position: absolute;
    right: 2.72rem;
    top: 0;
  }
  .image_6 {
    width: 4.69rem;
    height: 0.94rem;
    position: absolute;
    right: 8.28rem;
    bottom: 4.19rem;
  }
  .image_7 {
    width: 4.06rem;
    height: 0.59rem;
    position: absolute;
    right: 8.56rem;
    bottom: 3.94rem;
  }
  .image_8 {
    width: 4.47rem;
    height: 1.34rem;
    position: absolute;
    right: 8.5rem;
    bottom: 2.97rem;
  }
  .section_3 {
    padding: 0.22rem 0 0.13rem;
    background-image: url('https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/62ac1d455a7e3f03100b6468/62b188edd029820011534ee0/16558042050055452622.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    width: 9.34rem;
    position: absolute;
    right: -0.5rem;
    top: 27.72rem;
  }
  .image_10 {
    width: 8.81rem;
    height: 2.59rem;
    position: absolute;
    right: 0;
    bottom: 1.25rem;
  }
  .image_11 {
    width: 7.53rem;
    height: 3.16rem;
    position: absolute;
    right: 0;
    bottom: 0.53rem;
  }
  .image_12 {
    width: 3.22rem;
    height: 1.22rem;
    position: absolute;
    right: 2.56rem;
    bottom: 3.19rem;
  }
  .section_4 {
    padding: 1.06rem 0 3.09rem;
    background-image: url('https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/62ac1d455a7e3f03100b6468/62b188edd029820011534ee0/16558042053738232748.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    width: 6.09rem;
    position: absolute;
    right: 0.75rem;
    top: 22.06rem;
  }
  .image-wrapper_3 {
    padding: 1.09rem 0 1.56rem;
    background-image: url('https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/62ac1d455a7e3f03100b6468/62b188edd029820011534ee0/16558042056136709336.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    width: 3.09rem;
    position: absolute;
    right: 9.06rem;
    top: 24.84rem;
  }
  .image_17 {
    width: 3.28rem;
    height: 2.47rem;
    position: absolute;
    right: -2.16rem;
    bottom: 6.06rem;
  }
  .image_18 {
    width: 1.13rem;
    height: 1.13rem;
    position: absolute;
    right: 0.63rem;
    bottom: 7.78rem;
  }
  .section_5 {
    padding: 1.66rem 1.56rem 1.88rem;
    filter: drop-shadow(-0.011rem 0.31rem 1.53rem #c87d4d78);
    border-radius: 0.47rem;
   background: #fff;
    width: 20rem;
    position: absolute;
    top: 0.72rem;
    left: 50%;
    transform: translateX(-50%);
  }
  .group_4 {
    padding-bottom: 0.56rem;
    position: relative;
  }
  .group_5 {
    margin-top:2.5rem;
    padding-left: 0.66rem;
  }
  .image-wrapper_1 {
    margin-right: 3.44rem;
    padding: 1.44rem 0 2.19rem;
    background-image: url('https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/62ac1d455a7e3f03100b6468/62b188edd029820011534ee0/16558042054761417108.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    width: 3.25rem;
  }
  .image-wrapper_2 {
    margin-left: 0.41rem;
    margin-right: 0.34rem;
    padding-top: 0.31rem;
    background-image: url('https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/62ac1d455a7e3f03100b6468/62b188edd029820011534ee0/16558042052342064825.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    width: 8.59rem;
  }
  .image_15 {
    align-self: center;
    width: 1.03rem;
    height: 1.09rem;
  }
  .image_16 {
    width: 0.53rem;
    height: 0.53rem;
  }
  .text_4 {
    align-self: center;
    color: #333333;
    font-size: 1.19rem;
    font-family: SourceHanSansCN;
    font-weight: 700;
    line-height: 1.13rem;
    margin-bottom: 25rpx;
  }
  .image_19 {
    margin-top: 1.53rem;
    align-self: center;
    width: 10.38rem;
    height: 10.38rem;
  }
  .group_8 {
    margin-top: 1.06rem;
  }
  .image_2 {
    margin-right: 0.094rem;
    width: 2.47rem;
    height: 1.75rem;
  }
  .text_1 {
    color: #fff;
    font-size: 2.44rem;
    font-family: HuXiaoBo;
    font-weight: 700;
    line-height: 2.34rem;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    position: absolute;
  }
  .text_3 {
    margin-left: 0.66rem;
    color: #fff;
    font-size: 1rem;
    font-family: SourceHanSansCN;
    font-weight: 500;
    line-height: 0.94rem;
  }
  .view_1 {
    margin-left: 0.56rem;
  }
  .image_4 {
    width: 0.72rem;
    height: 0.78rem;
  }
  .image_9 {
    width: 8.13rem;
    height: 1.19rem;
  }
  .image_13 {
    width: 1.13rem;
    height: 1.13rem;
  }
  .image_14 {
    margin-left: 0.56rem;
    margin-top: 0.56rem;
    width: 0.59rem;
    height: 0.59rem;
  }
  .image_20 {
    width: 16.69rem;
    height: 0.031rem;
  }
  .text_5 {
    margin-top: 1.28rem;
    color: #ffffff;
    font-size: 0.88rem;
    font-family: SourceHanSansCN;
    line-height: 0.84rem;
  }
  .button {
    margin-top: 1.28rem;
    padding: 0.63rem 0;
    background-color: #D43E3F;
    box-shadow: 0px 0.094rem 0.56rem #e2a48536;
    border-radius: 1.09rem;
    width: 8.13rem;
  }
  .text_6 {
    color: #ffffff;
    font-size: 1rem;
    font-family: SourceHanSansCN;
    font-weight: 500;
    line-height: 0.94rem;
  }
</style>

